import axiosAPI from ".."
import appStore from "@/stores"
const { switchTipStatus } = appStore.homeviewStore

/**
 * 获取所有标签
 * @param {function} callback - 成功回调，参数为标签数据
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function GetTagsAll(callback, errorback) {
  axiosAPI("get", "/api/tags/")
    .then((rData) => {
      callback(rData)
    })
    .catch((error) => {
      switchTipStatus({ level: "warning", msg: "获取所有标签失败！请重试！" })
    })
}

/**
 * 新增标签
 * @param {Object} data {name: ""} - 标签数据
 * @param {function} callback - 成功回调，参数为响应数据
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function AddTag(data, callback, errorback) {
  axiosAPI("post", "/api/tags/", {}, {}, data)
    .then((rData) => {
      callback(rData)
      switchTipStatus({ level: "info", msg: "标签创建成功！" })
    })
    .catch((error) => {
      switchTipStatus({ level: "warning", msg: "标签创建失败！请重试！" })
      errorback(error)
    })
}

/**
 * 删除标签
 * @param {Object} data {name: ""} - 标签数据
 * @param {function} callback - 成功回调，参数为响应数据
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function DeleteTag(data, callback, errorback) {
  axiosAPI("delete", "/api/tags/", {}, {}, data)
    .then((rData) => {
      callback(rData)
    })
    .catch((error) => {
      switchTipStatus({ level: "warning", msg: "标签删除失败！请重试！" })
      // alert("error", "Delete tag Infomation", `delete tag-${data.name} fail! Please try again!`)
      //   errorback(error)
    })
}

/**
 * 获取某标签下的所有博客
 * @param {string} name - 标签名
 * @param {function} callback - 成功回调，参数为博客数据
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function TagToBlogs(name, callback, errorback) {
  axiosAPI("get", "/api/tags/" + name, {}, {}, {})
    .then((rData) => {
      callback(rData)
    })
    .catch((error) => {
      switchTipStatus({ level: "warning", msg: "检索博客失败！请重试！" })
      // alert("error", "Delete tag Infomation", `delete tag-${data.name} fail! Please try again!`)
      //   errorback(error)
    })
}
